import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


//js封装返回res 
declare module "axios" {
    interface AxiosResponse<T = any> {
        errorinfo: null;
        data: T;
        code: number;
        msg: string,
        count:number
        // 这里追加你的参数
    }
    export function create(config?: AxiosRequestConfig): AxiosInstance;
}


const requests = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_URL,
    timeout: 5000,
})
requests.interceptors.request.use((config) => {
    NProgress.start()
    return config;
})
requests.interceptors.response.use((res) => {
    NProgress.done()
    let { data, msg, code } = res.data
    /**
     * 拦截 session 过期
     */
    if (msg =="NOTLOGIN" && code == 0) {
       (window as any).top.location.href = "/login"
    }
    /**
     * 防止后端返回值不携带data而引起的报错
     */
    !res.data.data ? res.data.data = [] : undefined
    return res.data;
}, (error) => {
    NProgress.done()
    return {
        code: 500,
        msg: "系统响应超时",
        errorMessage: error
    }

})
export default requests;